package com.egame.manage.biz.server.gc;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.model.PageData;
import cn.egame.interfaces.gc.TerminalInfo;
import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.op.OperationActivityServiceImpl;
import com.egame.manage.biz.domain.TerminalBean;
import com.egame.manage.biz.domain.TerminalGroupInfoBean;

public class TerminalServiceImpl {
	private static TerminalServiceImpl instance = null;
	private static byte[] SyncRoot = new byte[1];
	private TerminalServiceDao dao = null;
	
	public static TerminalServiceImpl getInstance() throws ExceptionCommonBase {
		if (instance == null) {
			synchronized (SyncRoot) {
				if (instance == null) {
					instance = new TerminalServiceImpl();
				}
			}
		}
		return instance;
	}
	
	public TerminalServiceImpl() throws ExceptionCommonBase{
		super();
		dao = new TerminalServiceDao();
	}
	
	public int setTerminalInfo(int appId, long loginUserId, TerminalInfo terminalInfo) throws ExceptionCommonBase{
		return dao.setTerminalInfo(terminalInfo);
	}
	
	public int updateTerminalBean(int appId, long loginUserId, TerminalBean bean) throws ExceptionCommonBase{
		return dao.updateTerminalBean(bean);
	}
	
	public int deleteTerminalInfo(int appId, long loginUserId, int terminalId) throws ExceptionCommonBase {
		return  dao.deleteTerminalInfo(terminalId);
	}
	
	public List<TerminalInfo> listTerminalIdsByAdapted(int appId, long loginUserId,int gId,long fileId)throws RemoteException{
		List<Integer> ids = dao.listTerminalIdsByAdapted(gId, fileId);
		return ManageClient.getInstance().listTerminalInfoByIds(appId, loginUserId, ids);
	}
//	
//	public List<TerminalInfo> listTerminalByNotAdapted(int appId, long loginUserId,int gId,long fileId)throws RemoteException{
//		List<Integer> ids = dao.listTerminalByNotAdapted(gId, fileId);
//		return ManageClient.getInstance().listTerminalInfoByIds(appId, loginUserId, ids);
//	}
//		
//	public PageData pageTerminalInfo(int appId, long loginUserId,int gId,long fileId,int page,int rowsOfPage)throws RemoteException{
//		List<Integer> ids = dao.listTerminalIds(gId, fileId, page, rowsOfPage);
//		List<TerminalInfo> list = ManageClient.getInstance().listTerminalInfoByIds(appId, loginUserId, ids);
//		int total = dao.terminalCount(gId, fileId);
//		PageData pd = new PageData(page, total, rowsOfPage);
//		pd.setContent(list);
//		return pd;
//	}
	
	public PageData pageTerminalInfo(int appId, long loginUserId,int gId,long fileId,int manufactureId, int adapted,int page,int rowsOfPage)throws RemoteException{
		List<Integer> ids = new ArrayList<Integer>();
		ids = dao.listTerminalIds(gId, fileId, manufactureId, adapted, page, rowsOfPage);
		List<TerminalInfo> list = new ArrayList<TerminalInfo>();
		for(int id:ids){
			list.add(ManageClient.getInstance().getTerminalInfoById(appId, loginUserId, id));
		}
		int total = dao.getTerminalCount(gId, fileId, manufactureId, adapted);
		PageData pd = new PageData(page, total, rowsOfPage);
		pd.setContent(list);
		return pd;
	}

	public int setTerminalFileInfo(int appId, long loginUserId, int gid,
			long fileid, int terminalId, int terminalGroupId)throws ExceptionCommonBase {
		return dao.setTerminalFileInfo(gid,fileid,terminalId,terminalGroupId);
	}

	public List<TerminalInfo> listTerminalInfo(int appId, long loginUserId)throws RemoteException{
		List<Integer> ids = dao.listTerminalIds();
		return ManageClient.getInstance().listTerminalInfoByIds(appId, loginUserId, ids);
	}
		
	public PageData pageTerminalInfo(int appId, long loginUserId,int activityId,int page,int rowsOfPage)throws RemoteException{
		
		List<Integer> ids = this.listTerminalIds(appId,loginUserId,activityId, page, rowsOfPage);		
		List<TerminalInfo> list = ManageClient.getInstance().listTerminalInfoByIds(appId, loginUserId, ids);
		int total = this.getTerminalCount(appId, loginUserId, activityId);
		PageData pd = new PageData(page, total, rowsOfPage);
		pd.setContent(list);
		return pd;
	}

	public List<TerminalInfo> listTerminalByNotAdapted(int appId, long loginUserId,int activityId)throws RemoteException{
		List<Integer> ids = dao.listTerminalByNotAdapted(activityId);
		return ManageClient.getInstance().listTerminalInfoByIds(appId, loginUserId, ids);
	}
	
	public List<Integer> listTerminalIds(int manufactureId)throws RemoteException{
		return dao.listTerminalIds(manufactureId);
	}
	
	public List<String> listTerminalNames()  throws RemoteException{
		return dao.listTerminalNames();
	}
	
	public List<String> listOsNames()  throws RemoteException{
		return dao.listOsNames();
	}
	
	public List<String> listScreenPxs()  throws RemoteException{
		return dao.listScreenPxs();
	}
	
	public Map<String,Object> listTerminalIdAndShortUa(String TerName,String OsName,String screenPx,String shortUa) throws RemoteException{
		return dao.listTerminalIdAndShortUa(TerName, OsName, screenPx, shortUa);
	}
	
	public PageData pageTerminalIds(int appId, long loginUserId,TerminalInfo bean,int page,int rowsOfPage)throws RemoteException{
		List<Integer> ids = dao.listTerminalIds(bean, page, rowsOfPage);
		List<TerminalInfo> list = ManageClient.getInstance().listTerminalInfoByIds(appId, loginUserId, ids);
		int total = dao.getTerminalCount(bean);
		PageData pd = new PageData(page, total, rowsOfPage);
		pd.setContent(list);
		return pd;
	}

	public TerminalInfo getTerminalInfo(int appId,long loginUserId,int id)throws RemoteException{
		TerminalInfo bean = ManageClient.getInstance().getTerminalInfoById(appId, loginUserId, id);
		return bean==null?new TerminalInfo():bean;
	}
	
	public boolean isTerminalExist(String uacode) throws ExceptionCommonBase{
		return dao.isTerminalExist(uacode);
	}

	public int deleteAdaptedTerminal(int appId,long loginUserId,int gid,long fileId,int terminalId)throws ExceptionCommonBase{
		return dao.deleteAdaptedTerminal(gid, fileId, terminalId);
	}

	public int deleteAdaptedTerminal(int appId,long loginUserId,int gid,long fileId)throws ExceptionCommonBase{
		return dao.deleteAdaptedTerminal(gid, fileId);
	}

	public List<Integer> listTerminalIds(int appId,long loginUserId,long fileid,int gid)throws ExceptionCommonBase{
		return dao.listTerminalIds(fileid,gid);
	}
	
	public List<Integer> listTerminalIds(List<Integer> ids) throws ExceptionCommonBase {
		return dao.listTerminalIds(ids);
	}
	
	public List<Integer> listTerminalIds(int appId,long loginUserId,int activityId,int page,int rowsOfPage)throws ExceptionCommonBase{
		List<Integer> ids = OperationActivityServiceImpl.getInstance().listTerminalIds(activityId);
		ids = this.listTerminalIds(ids);
		List<Integer> result = new ArrayList<Integer>();
		int size = ids.size()>(page*rowsOfPage+rowsOfPage)?(page*rowsOfPage+rowsOfPage):ids.size();
		if(ids != null && ids.size()>=page*rowsOfPage){
			for(int i=page*rowsOfPage;i<size;i++){
				result.add(ids.get(i));				
			}
			return result;
		}
		return new ArrayList<Integer>();
	}

	public int getTerminalCount(int appId,long loginUserId,int activityId)throws ExceptionCommonBase{
		List<Integer> ids = OperationActivityServiceImpl.getInstance().listTerminalIds(activityId);
		ids = this.listTerminalIds(ids);
		if(ids != null){
			return ids.size();
		}
		return 0;
	}
	
	public List<Integer> listTerminalInfoByNotManufactureId(int appId,long loginUserId,int manufactureId)throws RemoteException{
		List<Integer> allIds = dao.listTerminalIds();
		List<Integer> manuIds = dao.listTerminalIds(manufactureId);
		if(allIds != null && manuIds != null){
			for(Integer id:manuIds){
				if(allIds.contains(id)){
					allIds.remove(id);
				}
			}
			return allIds;
		}
		return new ArrayList<Integer>();
	}
	
	public List<Integer> listTerminalIds()throws ExceptionCommonBase{
		return dao.listTerminalIds();
	}
	
	public List<Integer> listTerminalIdsByGroupId(int terminalGroupId)
			throws ExceptionCommonBase{
		List<Integer> terminalIds = dao.listTerminalIdsByGroupId(terminalGroupId);
		return terminalIds;
	}
	
	public List<Integer> listTerminalIdsByNotGroupId(int terminalGroupId)
			throws ExceptionCommonBase{
		List<Integer> allIds = dao.listTerminalIds();
		List<Integer> relatedIds = dao.listTerminalIdsByGroupId(terminalGroupId);
		if(allIds != null && relatedIds != null){
			for(Integer id:relatedIds){
				if(allIds.contains(id)){
					allIds.remove(id);
				}
			}
			return allIds;
		}
		return new ArrayList<Integer>();
	}

}
